/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.muni.fi.pb138.videolibrary.GUI;

import cz.muni.fi.pb138.videolibrary.database.Category;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author vaculik
 */
public class CategoriesTableModel extends AbstractTableModel {

        private List<Category> categories;
        
        public CategoriesTableModel(List<Category> c) {
            if (c == null) {
                throw new NullPointerException("List of categories is null");
            }
            categories = sortCategoriesById(c);
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        @Override
        public int getRowCount() {
            return categories.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        public Category getCategory(int index) {
            return categories.get(index);
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (categories == null || categories.isEmpty()) {
                categories = new ArrayList<>(videolibrary.getAllCategories());
            }
            if (categories == null) {
                return null;
            }
            Category category = categories.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return category.getId();
                case 1:
                    return category.getTitle();
                default:
                    throw new IllegalArgumentException("columnIndex");
            }
        }

        @Override
        public String getColumnName(int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return "ID";
                case 1:
                    return "Title";
                default:
                    throw new IllegalArgumentException("columnIndex");
            }
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return long.class;
                case 1:
                    return String.class;
                default:
                    throw new IllegalArgumentException("columnIndex");
            }
        }

        public void setCategories(ArrayList<Category> categories) {
            this.categories = sortCategoriesById(categories);
            fireTableDataChanged();
        }

        public void removeAllCategories() {
            categories.clear();
            fireTableDataChanged();
        }

        public void removeCategories(ArrayList<Category> categories, int[] rows) {
            if (!categories.isEmpty()) {
                this.categories.removeAll(categories);
                fireTableRowsDeleted(rows[0], rows[rows.length - 1]);
            }
        }

        public void addCategory(Category category) {
            categories.add(category);
            int lastRow = categories.size() - 1;
            this.fireTableRowsInserted(lastRow, lastRow);
        }

        public void updateCategory(Category category) {
            int index = 0;
            for (Category tmp : categories) {
                if (category.getId() == tmp.getId()) {
                    break;
                }
                index++;
            }
            categories.set(index, category);
            this.fireTableRowsUpdated(index, index);
        }

        private List<Category> sortCategoriesById(List<Category> categories) {
            List<Category> sortedCategories = new ArrayList<>(categories);
            Collections.sort(sortedCategories, new IdComparator());
            return sortedCategories;
        }

        private class IdComparator implements Comparator<Category> {

            @Override
            public int compare(Category category1, Category category2) {
                if (category1.getId() > category2.getId()) {
                    return 1;
                } else if (category1.getId() < category2.getId()) {
                    return -1;
                } else {
                    throw new RuntimeException("Two categories with identical id have been found.");
                }
            }
        }
    }